package com.fitc.json;


import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


import com.fitc.imageprocessing.ColorData;
import com.fitc.tokeniser.Tokeniser;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;

public class Photo {
    private long id;
    private String title;
    private URL url_q;
    private String tags;
    private String processedTags;
    private String pathalias;
    private ColorData colorData;
    private boolean dud = false;
    
    public String getPathalias() {return pathalias; }
    public String getTitle() { return title; }
    public long getId() { return id; }
    public URL getUrl() { return url_q; }
    public String getTags() { return tags; }
    public ColorData getColorData() {return colorData;}
    public boolean isDud(){ return dud;}
 
    
    public void setPathalias(String pathalias) { this.pathalias = pathalias;}
    public void setTitle(String title) { this.title = title; }
    public void setId(long id) { this.id = id; }
    public void setUrl(URL url) { this.url_q = url; }
    public void setColorData(ColorData c) {this.colorData = c;}
    public void setAsDud(){ dud = true;}
    
   // public void setTags(String tags) { this.tags = tags; }
    @Override
    public String toString() {
        return String.format("id:%d,title:%s,tags:%s,url:%s", id, title, tags, url_q);
   }
    
    @Override
    public int hashCode() {
        return (int) id;
    }
    
    @Override
    public boolean equals(Object other){
    	if (other == null) return false;
        if (other == this) return true;
        if (!(other instanceof Photo))return false;
        Photo that = (Photo)other;
        if (this.id == that.id) return true;
    	return false;
    }

	
	public List<String> getTagsSplit(boolean includeTitle){
		 String s; 
		if (includeTitle) {
			s = tags + " " + title;
		} else {
			s = tags;
		}
		s = s.toLowerCase();
		
		ArrayList<String> tokens = new ArrayList<String>();
		for (String str : s.split("\\s")) {  
		    tokens.add(str);  
		} 
		
		ListIterator<String> iterator = tokens.listIterator();
		while (iterator.hasNext()){
			String str = iterator.next().replaceAll("[^A-Za-z]", "");
				
			if (str.length()<=2 || str.equals("")){
				iterator.remove();
			} else {
				iterator.set(str);
			}
			
		}
		return tokens;
	}
	
	public Photo(){
		this.colorData = new ColorData(this);
	}
	
	public String[] tokenisedTags(boolean includeTitle){
		
		 	String s; 
			if (includeTitle) {
				s = tags + " " + title;
			} else {
				s = tags;
			}
			s = s.toLowerCase();
		
			Tokeniser t = new Tokeniser(s);			
			return t.process();
	}
	
}
